package com.sqrj.phoneorder.Controller;

import com.sqrj.phoneorder.common.JsonUtils;
import com.sqrj.phoneorder.common.ServerResponse;
import com.sqrj.phoneorder.model.WxHntRWD;
import com.sqrj.phoneorder.model.WxHntRWDVo;
import com.sqrj.phoneorder.model.WxHntRWDVo1;
import com.sqrj.phoneorder.service.WxHntRwdService;
import com.sqrj.phoneorder.util.DateTimeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

@RestController
public class WxHntRWDController {

    @Resource
    private WxHntRwdService wxHntRwdService;

    //添加任务单
    @PostMapping("/addWxHntRWD")
    public ServerResponse addWxHntRWD(@RequestBody WxHntRWD WxHntRWD){

        if(WxHntRWD.getCode() == null ||"".equals(WxHntRWD.getCode())|| StringUtils.isEmpty(WxHntRWD.getCode())){
            WxHntRWD.setCode("未正常传入code");
        }
        String yq = WxHntRWD.getSpecialRequire();
        if(yq.indexOf("KS") == -1){
            WxHntRWD.setKsdj("抗渗无");
        };
        if(yq.indexOf("KSH") == -1){
            WxHntRWD.setKshedj("抗折无");
        };
        String ffdj = WxHntRWD.getFfdj();
        if(ffdj == null || "".equals(ffdj) || yq.indexOf("FF") == -1){
            WxHntRWD.setFfdj("防腐无");
        };
        String zxyq = WxHntRWD.getZxyq();
        if(zxyq == null || "".equals(zxyq) || yq.indexOf("ZX") == -1){
            WxHntRWD.setZxyq("阻锈无");
        };

        String qtyq = WxHntRWD.getOtherRequire();
        if(qtyq == null || "".equals(qtyq)){
            WxHntRWD.setOtherRequire("其他无");
        };


        return wxHntRwdService.addWxHntRWD(WxHntRWD);

    }

    //企业端查询单位
    @PostMapping("/queryWxUnitsByCode")
    public Map<String,Object> queryWxUnitsByCode(@RequestBody Map<String,Object> map){
        String wxUserCode = (String)map.get("code");
        Integer status = (Integer)map.get("status");
        String date = (String)map.get("date");
        String date1 = (String)map.get("date1");
        Map<String, Object> modelMap = new HashMap<String, Object>();
        List<String> wxUnitsList = wxHntRwdService.queryWxUnitsByCode(wxUserCode,status,date,date1);
        wxUnitsList.add(0,"全部");
        modelMap.put("wxUnitsLists",wxUnitsList);
        return  modelMap;
    }


    @PostMapping("/queryWxHntRWDbyUnit")
    public Map<String,Object> queryWxHntRWDbyUnit(@RequestBody Map<String,Object> map){
        //TODO 获取参数，可封装为一个参数类中
        Integer status = (Integer)map.get("status");
        String unitName =(String)map.get("unitName");
        String date = (String)map.get("date");
        String date1 = (String)map.get("date1");
        String code  = (String)map.get("code");
        //判断单位名称是否为“全部”,全部设为空查询全部的任务单
        if(unitName.equals("全部")){
            unitName = null;
        };
        Map<String, Object> modelMap = new HashMap<String, Object>();
        //查询任务单
        List<WxHntRWD> WxHntRWDList = wxHntRwdService.queryWxHntRWDbyUnit(code,status,unitName,date,date1);
        //TODO 可在实体类中修改返回格式字段，重构
        List<WxHntRWDVo> WxHntRWDList1 = new ArrayList<>();
        for (WxHntRWD wxHntRWD:WxHntRWDList) {
            WxHntRWDVo wxHntRWDVo = new WxHntRWDVo();
            String cesdate = DateTimeUtil.dateToStr(wxHntRWD.getXjTime(),"yyyy-MM-dd");
            java.util.Date datece =  DateTimeUtil.strToDate(cesdate,"yyyy-MM-dd");
            wxHntRWDVo.setId(wxHntRWD.getId());
            wxHntRWDVo.setSupplyTime(DateTimeUtil.dateToStr(wxHntRWD.getSupplyTime(),"MM月dd日"));
            wxHntRWDVo.setWxproName(wxHntRWD.getWxproName());
            wxHntRWDVo.setPouringSite(wxHntRWD.getPouringSite());
            wxHntRWDVo.setQddj(wxHntRWD.getQddj());
            wxHntRWDVo.setKsdj(wxHntRWD.getKsdj());
            wxHntRWDVo.setKshedj(wxHntRWD.getKshedj());
            wxHntRWDVo.setFfdj(wxHntRWD.getFfdj());
            wxHntRWDVo.setZxyq(wxHntRWD.getZxyq());
            wxHntRWDVo.setSpecialRequire(wxHntRWD.getSpecialRequire());
            wxHntRWDVo.setOtherRequire(wxHntRWD.getOtherRequire());
            wxHntRWDVo.setBusinessAgent(wxHntRWD.getBusinessAgent());
            wxHntRWDVo.setContactPhone(wxHntRWD.getContactPhone());
            wxHntRWDVo.setStationPhone(wxHntRWD.getStationPhone());
            wxHntRWDVo.setWxunitOpenid(wxHntRWD.getWxunitOpenid());
            wxHntRWDVo.setStationName(wxHntRWD.getStationName());
            wxHntRWDVo.setWxunitName(wxHntRWD.getWxunitName());
            if(wxHntRWD.getOrderQuantity() != null) {
                wxHntRWDVo.setOrderQuantity(wxHntRWD.getOrderQuantity());
            }else{
                wxHntRWDVo.setOrderQuantity(0.0);
            }
            wxHntRWDVo.setCode(wxHntRWD.getCode());
            wxHntRWDVo.setXjTime(DateTimeUtil.dateToStr(wxHntRWD.getXjTime(),"MM月dd日"));
            wxHntRWDVo.setIsself(false);
            wxHntRWDVo.setOrderConno(wxHntRWD.getOrderConno());
            WxHntRWDList1.add(wxHntRWDVo);
        }

        modelMap.put("WxHntRWDList",WxHntRWDList1);
        return  modelMap;

    }

    //查询混凝土任务单
    @PostMapping("/queryWxHntRWD")
    public Map<String,Object> queryWxHntRWDbyStatus(@RequestBody Map<String,Object> map){
        Integer status = (Integer)map.get("status");
        String openId = (String)map.get("openId");
        String unitName =(String)map.get("unitName");
        if(unitName.equals("全部")){
            unitName = null;
        }
        Map<String, Object> modelMap = new HashMap<String, Object>();
        List<WxHntRWD> WxHntRWDList = wxHntRwdService.queryWxHntRWDbyStatus(status,unitName);
        List<WxHntRWDVo> WxHntRWDList1 = new ArrayList<>();
        for (WxHntRWD wxHntRWD:WxHntRWDList) {
            WxHntRWDVo wxHntRWDVo = new WxHntRWDVo();
            String cesdate = DateTimeUtil.dateToStr(wxHntRWD.getXjTime(),"yyyy-MM-dd");
            java.util.Date datece =  DateTimeUtil.strToDate(cesdate,"yyyy-MM-dd");
            wxHntRWDVo.setId(wxHntRWD.getId());
            wxHntRWDVo.setSupplyTime(DateTimeUtil.dateToStr(wxHntRWD.getSupplyTime(),"MM月dd日"));
            wxHntRWDVo.setWxproName(wxHntRWD.getWxproName());
            wxHntRWDVo.setPouringSite(wxHntRWD.getPouringSite());
            wxHntRWDVo.setQddj(wxHntRWD.getQddj());
            wxHntRWDVo.setKsdj(wxHntRWD.getKsdj());
            wxHntRWDVo.setKshedj(wxHntRWD.getKshedj());
            wxHntRWDVo.setFfdj(wxHntRWD.getFfdj());
            wxHntRWDVo.setZxyq(wxHntRWD.getZxyq());
            wxHntRWDVo.setSpecialRequire(wxHntRWD.getSpecialRequire());
            wxHntRWDVo.setOtherRequire(wxHntRWD.getOtherRequire());
            wxHntRWDVo.setBusinessAgent(wxHntRWD.getBusinessAgent());
            wxHntRWDVo.setContactPhone(wxHntRWD.getContactPhone());
            wxHntRWDVo.setStationPhone(wxHntRWD.getStationPhone());
            wxHntRWDVo.setWxunitOpenid(wxHntRWD.getWxunitOpenid());
            wxHntRWDVo.setStationName(wxHntRWD.getStationName());
            wxHntRWDVo.setCode(wxHntRWD.getCode());
            wxHntRWDVo.setXjTime(DateTimeUtil.dateToStr(wxHntRWD.getXjTime(),"MM月dd日"));
            if(openId == null || "".equals(openId) ){
                wxHntRWDVo.setIsself(false);
            }else {
                wxHntRWDVo.setIsself(openId.equals(wxHntRWD.getWxunitOpenid()));
            };
            wxHntRWDVo.setOrderConno(wxHntRWD.getOrderConno());
            WxHntRWDList1.add(wxHntRWDVo);
        }

        modelMap.put("WxHntRWDList",WxHntRWDList1);
        return  modelMap;

    }


}
